package com.dd.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.dd.entity.*;
import com.dd.util.PageList;

public class DdQaDAO extends HibernateDaoSupport  {
	private static final Log log = LogFactory.getLog(DdQaDAO.class);
	public void saveOrUpdate(DdQa transientInstance) {
		log.debug("saving DdSms instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	public void delete(DdQa persistentInstance) {
		log.debug("deleting DdSms instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	

	public DdQa findById(java.lang.Integer id) {
		log.debug("findById DdQa with id: " + id);
		try {
			DdQa instance = (DdQa) getHibernateTemplate().get(
					"com.dd.entity.DdQa", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public DdQa findLastByUserId(java.lang.Integer uid) {
		log.debug(" findLastByUserId with id: " + uid);
		try {
			List list = getHibernateTemplate().find("from DdQa where userid="+uid +" order by id  desc");
			if(list.size()>0){
				return (DdQa)list.get(0);
			}else{
				return null;
			}
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public List findToAnswer() {
		log.debug("findToAnswer DdQa ");
		try {
			return getHibernateTemplate().find("from DdQa where answerid=0");
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public PageList findAnswered(int pageNo, int pageSize) {
		log.debug("findAnswered DdQa");
		try {
			PageList pl = new PageList();
			String queryString = "from DdQa where answerid!=0  ";
					
			String maxsizeStr = (" select count(*) " + queryString);
			Query q = this.getSession().createQuery(maxsizeStr);
			int max = 0;
			List l = q.list();
			if (l != null && !l.isEmpty()) {
				max = (Integer) l.get(0);
			}
			q = this.getSession().createQuery("from DdQa where answerid!=0 order by id desc");
			q.setFirstResult((pageNo - 1) * pageSize);
			q.setMaxResults(pageSize);
			List list = q.list();
			pl.addAll(list);
			pl.setTotalCount(max);
			return pl;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public PageList findAll(int pageNo, int pageSize) {
		log.debug("findAnswered DdQa");
		try {
			PageList pl = new PageList();
			String queryString = "from dd_qa";
					
			String maxsizeStr = (" select count(*) " + queryString);
			Query q = this.getSession().createQuery(maxsizeStr);
			int max = 0;
			List l = q.list();
			if (l != null && !l.isEmpty()) {
				max = (Integer) l.get(0);
			}
			q = this.getSession().createQuery("from DdQa order by id desc");
			q.setFirstResult((pageNo - 1) * pageSize);
			q.setMaxResults(pageSize);
			List list = q.list();
			pl.addAll(list);
			pl.setTotalCount(max);
			return pl;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	
}
